#include "form_xmdz.h"
#include "ui_form_xmdz.h"
#include "pub_var.h"
#include <QSqlRecord>
#include <QMessageBox>
#include <QSqlError>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>

Form_xmdz::Form_xmdz(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_xmdz)
{
    ui->setupUi(this);

    bool windInitialed = false;;
    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/xmdz").toRect();
    this->setGeometry(rct);

    //数据库连接
    qro = new QSqlQuery(pub::db_o);
    qrm = new QSqlQuery(pub::db_m);

    //状态显示
    //ui->leStatus->setEnabled(false);
    //ui->dtStart->setDate(QDate::currentDate());

    //查询管理权限标志
    qro->exec("select dept_type from com_department where dept_type = 'A' and dept_code in"
              " ( select distinct p.dept_code from com_priv_user p where "
              "  p.user_code = '" + pub::usr_code + "')");

    if(qro->next()){
        allPriv = true;
    }else{
        allPriv = false;
    }


    qrm->exec("select code,value from new_dict_all where type = 'list_type' and valid_flag = '1'");
    ui->cbbList->clear();
    QString itm;
    while(qrm->next()){
        itm = qrm->value(0).toString().trimmed() ;
        itm.append(" ");
        itm.append(qrm->value(1).toString().trimmed());
        ui->cbbList->addItem(itm);
    }

    ui->pbTest->setVisible(false);

    tm = new QTimer(this);

    connect(tm, SIGNAL(timeout()), this, SLOT(onTimeOut()));

    //网络参数
    request.setUrl(QUrl(pub::hos_ybjy));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    //QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    //QNetworkAccessManager* na1301 = new QNetworkAccessManager(this);
    ui->cbbList->setCurrentIndex(-1);
    windInitialed = true;

    connect(ui->cbbList,SIGNAL(currentIndexChanged(int)),this,SLOT(on_cbbList_curIndChanged(int)));
    //connect(ui->twDpList, SIGNAL(itemClicked(QTableWidgetItem *)),this,SLOT(twDpList_Clicked(QTableWidgetItem *)));
    //connect(ui->cbbList,&QComboBox::currentIndexChanged,this,&Form_xmdz::on_cbbList_curIndChanged);   //
    //QNetworkAccessManager* naaManager0 = new QNetworkAccessManager(this);
}

Form_xmdz::~Form_xmdz()
{
    delete ui;
}

void Form_xmdz::on_pbExit_clicked()
{
    this->close();
}

void Form_xmdz::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/xmdz", QVariant(this->geometry()));

}

void Form_xmdz::on_rbLocal_clicked()
{
    display(ui->leSearch->text());
}

void Form_xmdz::on_rbCenter_clicked()
{
    display(ui->leSearch->text());
}

void Form_xmdz::on_rbCompared_clicked()
{
    display(ui->leSearch->text());
}

void Form_xmdz::on_cbbList_curIndChanged(int)
{
    //if( !ui->cbbList->currentText().isEmpty() ){    //
    display(ui->leSearch->text());
    //}

}

void Form_xmdz::onTimeOut()
{
    tm->stop();
    display(ui->leSearch->text());
}

void Form_xmdz::on_leSearch_textChanged(QString s)
{
    tm->stop();
    tm->start(500);
}

void Form_xmdz::display(QString key)
{
    mdList.setQuery(NULL);  //清空当前显示
    qApp->processEvents();

    list_type = pub::getStr(ui->cbbList->currentText()," ",1); //目录类别

    if(!allPriv && list_type != "999"){ //无完整权限只能操作 诊断 类别
        return;
    }

    if(ui->rbLocal->isChecked()){   //当前显示本地目录
        if(list_type == "999"){  // 疾病名称
            sq = " select 疾病编码,疾病名称,拼音助记码 from si_bingzhongmulu  "
                 " where (疾病名称 like '%"
                    +key +"%' or 拼音助记码 like upper('%"
                    +key +"%') ) order by 疾病编码 ";
            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "疾病编码");
            mdList.setHeaderData(1, Qt::Horizontal, "疾病名称");
            mdList.setHeaderData(2, Qt::Horizontal, "拼音助记码");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.3);
            ui->tvDisplay->setColumnWidth(1, width * 0.3);
            ui->tvDisplay->setColumnWidth(2, width * 0.3);
            ui->tvDisplay->setColumnWidth(3, 0);
            ui->tvDisplay->setColumnWidth(4, 0);

        }else if(list_type == "201"){  //诊疗项目
            sq = " select item_code,item_name,oper_date, fee_code,stock_unit,unit_price, "
                 " spell_code, valid_state from fin_com_undruginfo "
                 " where (spell_code like upper('%"
                    +key+"%') or item_name like '%"
                    +key+"%' ) and valid_state = '1'"
                         " and (siundrug_type is null or siundrug_type <>'1' )  order by item_name ";
            //(compared is null or compared = '0') and
            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "项目ID");
            mdList.setHeaderData(1, Qt::Horizontal, "项目名称");
            mdList.setHeaderData(2, Qt::Horizontal, "开始时间");
            mdList.setHeaderData(3, Qt::Horizontal, "费用类别");
            mdList.setHeaderData(4, Qt::Horizontal, "单位");
            mdList.setHeaderData(5, Qt::Horizontal, "价格");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);
        }else if(list_type == "101"){  //中西药
            sq = " select drug_code,trade_name, approve_info,fee_code,specs,retail_price,pack_unit,"
                 " pack_qty from pha_com_baseinfo"
                 " where fee_code in ('001','002') and (compared is null or compared = '0') "
                 "and (spell_code like upper('%"
                    +key +"%') or trade_name like '%"
                    +key +"%') and valid_state = '1' order by trade_name";
            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "药品ID");
            mdList.setHeaderData(1, Qt::Horizontal, "药品名称");
            mdList.setHeaderData(2, Qt::Horizontal, "批准文号");
            mdList.setHeaderData(3, Qt::Horizontal, "费用类别");
            mdList.setHeaderData(4, Qt::Horizontal, "规格");
            mdList.setHeaderData(5, Qt::Horizontal, "包装");
            mdList.setHeaderData(6, Qt::Horizontal, "价格");

            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, width * 0.1);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);


        }else if(list_type == "103"){  //院内制剂
            sq = " select drug_code,trade_name, create_time,fee_code,specs,retail_price,pack_unit,"
                 " pack_qty from pha_com_baseinfo"
                 " where fee_code in ('001','002') and (compared is null or compared = '0') "
                 "and spell_code like upper('%"+key+"%') and valid_state = '1' and self_flag = '1' order by trade_name";
            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "药品ID");
            mdList.setHeaderData(1, Qt::Horizontal, "药品名称");
            mdList.setHeaderData(2, Qt::Horizontal, "开始时间");
            mdList.setHeaderData(3, Qt::Horizontal, "费用类别");
            mdList.setHeaderData(4, Qt::Horizontal, "规格");
            mdList.setHeaderData(5, Qt::Horizontal, "包装");
            mdList.setHeaderData(6, Qt::Horizontal, "价格");

            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, width * 0.1);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);
        }else if(list_type == "301"){  //耗材
            sq = " select item_code,item_name,oper_date, fee_code,stock_unit,unit_price, "
                 " spell_code, valid_state from fin_com_undruginfo "
                 " where (compared is null or compared = '0') and valid_state = '1'"
                 " and spell_code like upper('%"+key+"%') and siundrug_type = '1' order by item_name ";

            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "项目ID");
            mdList.setHeaderData(1, Qt::Horizontal, "项目名称");
            mdList.setHeaderData(2, Qt::Horizontal, "开始时间");
            mdList.setHeaderData(3, Qt::Horizontal, "费用类别");
            mdList.setHeaderData(4, Qt::Horizontal, "单位");
            mdList.setHeaderData(5, Qt::Horizontal, "价格");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);

        }

    }else if(ui->rbCenter->isChecked()){//当前显示中心目录
        if(list_type == "999"){  // 疾病名称
            sq = " select 章名称,节名称,类目名称,亚目名称,诊断名称,诊断代码 from new_down_1307 "
                 " where 诊断名称 <>'' and 诊断代码 <>'' and 诊断名称 like upper('%"
                    +key
                    +"%') order by 诊断代码 ";
            mdList.setQuery(sq, pub::db_m);
            mdList.setHeaderData(0, Qt::Horizontal, "章名称");
            mdList.setHeaderData(1, Qt::Horizontal, "节名称");
            mdList.setHeaderData(2, Qt::Horizontal, "类目名称");
            mdList.setHeaderData(3, Qt::Horizontal, "亚目名称");
            mdList.setHeaderData(4, Qt::Horizontal, "诊断名称");
            mdList.setHeaderData(5, Qt::Horizontal, "诊断代码");

            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.15);
            ui->tvDisplay->setColumnWidth(3, width * 0.15);
            ui->tvDisplay->setColumnWidth(4, width * 0.2);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
        }else if(list_type == "101"){  //中西药
            sq = " select 医疗目录编码, 注册名称, 批准文号, 生产企业名称, 药品规格, 最小包装数量, 最小包装单位 "
                 " from new_down_1301_simple where 批准文号 like '%"
                    + key + "%' or 注册名称 like '%"
                    + key + "%'"
            //" and 结束日期 > curdate() or 结束日期 is null or 结束日期 = 'null' ";
            " order by 生产企业名称, 药品规格";  //_simple
            mdList.setQuery(sq, pub::db_m);
            mdList.setHeaderData(0, Qt::Horizontal, "药品ID");
            mdList.setHeaderData(1, Qt::Horizontal, "药品名称");
            mdList.setHeaderData(2, Qt::Horizontal, "批准文号");
            mdList.setHeaderData(3, Qt::Horizontal, "生产企业");
            mdList.setHeaderData(4, Qt::Horizontal, "规格");
            mdList.setHeaderData(5, Qt::Horizontal, "包装");
            mdList.setHeaderData(6, Qt::Horizontal, "单位");

            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, width * 0.1);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);


        }else if(list_type == "201"){  //诊疗项目
            sq = " select 医疗目录编码, 医疗服务项目名称, 诊疗项目内涵,诊疗项目说明 "
                 "from new_down_1305 "
                 "where 医疗服务项目名称 like '%"+key+"%'";
            //and 结束日期 = 'null' or 结束日期 is null or 结束日期 >curdate() "  ;

            mdList.setQuery(sq, pub::db_m);
            mdList.setHeaderData(0, Qt::Horizontal, "项目ID");
            mdList.setHeaderData(1, Qt::Horizontal, "项目名称");
            mdList.setHeaderData(2, Qt::Horizontal, "项目内容");
            mdList.setHeaderData(3, Qt::Horizontal, "说明");
            //mdList.setHeaderData(4, Qt::Horizontal, "");
            //mdList.setHeaderData(5, Qt::Horizontal, "价格");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.3);
            ui->tvDisplay->setColumnWidth(1, width * 0.3);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.2);
            ui->tvDisplay->setColumnWidth(4, 0);
            ui->tvDisplay->setColumnWidth(5, 0);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);

        }else if(list_type == "103"){  //院内制剂
            sq = " select 医疗目录编码,药品商品名,批准文号,剂型,药品规格,国家医保药品目录编号 "
                 " from new_down_1303 where 药品商品名 like '%"+key+"%' "
                    //" where 结束日期 > curdate() or 结束日期 is null or 结束日期 = 'null' "
                    " order by 药品商品名";;
            mdList.setQuery(sq, pub::db_m);
            mdList.setHeaderData(0, Qt::Horizontal, "药品ID");
            mdList.setHeaderData(1, Qt::Horizontal, "药品名称");
            mdList.setHeaderData(2, Qt::Horizontal, "批准文号");
            mdList.setHeaderData(3, Qt::Horizontal, "剂型");
            mdList.setHeaderData(4, Qt::Horizontal, "规格");
            //mdList.setHeaderData(5, Qt::Horizontal, "包装");
            //mdList.setHeaderData(6, Qt::Horizontal, "价格");

            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.2);
            ui->tvDisplay->setColumnWidth(4, width * 0.2);
            ui->tvDisplay->setColumnWidth(5, 0);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);
        }else if(list_type == "301"){  //耗材
            sq = "select 医疗目录编码,耗材名称,规格,注册备案号,生产企业名称"
                 " from new_down_1306_simple where "
                 " 耗材名称 like '%" + key + "%' or 注册备案号 like '%" + key + "%'";
            mdList.setQuery(sq, pub::db_m);
            mdList.setHeaderData(0, Qt::Horizontal, "项目ID");
            mdList.setHeaderData(1, Qt::Horizontal, "项目名称");
            mdList.setHeaderData(2, Qt::Horizontal, "规格");
            mdList.setHeaderData(3, Qt::Horizontal, "注册证号");
            mdList.setHeaderData(4, Qt::Horizontal, "生产企业");
            //mdList.setHeaderData(5, Qt::Horizontal, "价格");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.2);
            ui->tvDisplay->setColumnWidth(4, width * 0.2);
            ui->tvDisplay->setColumnWidth(5, 0);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);

        }

    }else if(ui->rbCompared->isChecked()){//当前显示已对照目录
        if(list_type != "999"){
            sq = "select fixmedins_hilist_id,fixmedins_hilist_name,med_list_codg,center_name"
                 " from fin_com_compare where new_flag = '1' and list_type = '"
                    + list_type
                    + "' and (his_spell like '%"
                    + key
                    + "%' or his_name like '%"
                    + key + "%') ";
            //center_name

            //sq = "select medins_list_codg, med_list_codg, medins_list_name"
            //     " from new_down_1317 where medins_list_name like '%"+key+"%' and list_type = '" + list_type + "'";

            mdList.setQuery(sq, pub::db_o);
            mdList.setHeaderData(0, Qt::Horizontal, "本地代码");
            mdList.setHeaderData(1, Qt::Horizontal, "本地名称");
            mdList.setHeaderData(2, Qt::Horizontal, "中心代码");
            mdList.setHeaderData(3, Qt::Horizontal, "中心名称");
            //mdList.setHeaderData(4, Qt::Horizontal, "单位");
            //mdList.setHeaderData(5, Qt::Horizontal, "价格");
            double width = ui->tvDisplay->width() - 50;
            ui->tvDisplay->setModel(&mdList);
            ui->tvDisplay->setColumnWidth(0, width * 0.2);
            ui->tvDisplay->setColumnWidth(1, width * 0.2);
            ui->tvDisplay->setColumnWidth(2, width * 0.2);
            ui->tvDisplay->setColumnWidth(3, width * 0.1);
            ui->tvDisplay->setColumnWidth(4, width * 0.1);
            ui->tvDisplay->setColumnWidth(5, width * 0.1);
            ui->tvDisplay->setColumnWidth(6, 0);
            ui->tvDisplay->setColumnWidth(7, 0);
            ui->tvDisplay->setColumnWidth(8, 0);
            ui->tvDisplay->setColumnWidth(9, 0);
            ui->tvDisplay->setColumnWidth(10, 0);
            ui->tvDisplay->setColumnWidth(11, 0);
            ui->tvDisplay->setColumnWidth(12, 0);
            ui->tvDisplay->setColumnWidth(13, 0);
        }
    }


}
void Form_xmdz::on_pbCompare_clicked()
{
    if(list_type =="999"){
        if(centerID.isEmpty()){
            QMessageBox::information(this,"note","未选定中心项目！");
            return;
        }
        sq = "insert into si_bingzhongmulu(疾病编码,疾病名称,开始时间,有效标志,生育使用标志,疾病种类,拼音助记码)"
             "values"
             "('"+ centerID
                +"','" + hilist_name
                +"','" + QDateTime::currentDateTime().toString("yyyyMMddHHmmss")
                + "','1','1','13'"
                +",'" + ui->lePinyin->text() + "')";
        if(!qro->exec(sq)){
            QMessageBox::information(this,"note","保存数据失败！");
            qDebug()<<qro->lastQuery();
        }else{
            QMessageBox::information(this,"note","项目数据保存完成！");
        }
        return;
    }
    QJsonObject in; //入参
    QJsonObject inpu,inpud; // 入参 input 段
    QJsonArray jsar;
    QJsonDocument indoc ;



//    //检查医保目录是否存在此项目
//    putBaseIn(in, "1312");

//    inpu.insert("hilist_code", centerID);//项目代码
//    inpu.insert("updt_time", "2010-01-01 00:00:00");   //更新时间
//    inpu.insert("page_num", 1);    //当前页数
//    inpu.insert("page_size", 50);   //本页数据量
//    inpu.insert("vali_flag", "1");   //
//    inpu.insert("insu_admdvs", pub::hos_insuplc);   //
//    inpu.insert("list_type", list_type);   //


//    inpud.insert("data",inpu);
//    in.insert("input", inpud);

//    indoc.setObject(in);

//    qDebug()<<"1312 交易入参: "<<indoc;

//    //保存入参日志
//    sq = "update com_business set v_in = '"
//            + indoc.toJson(QJsonDocument::Compact)
//            + "' where jyls = '" + currentMsgid + "'";

//    if(!qrm->exec(sq)){
//        QMessageBox::information(this,"错误","保存入参日志失败！");
//        qDebug()<<sq;
//    }

//    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
//    QNetworkReply* reply = naManager->post(request,indoc.toJson());

//    QEventLoop eventLoop;
//    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
//    eventLoop.exec(); // 进入等待返回

//    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

//    if(!statusCode.isValid()){
//        QMessageBox::warning(NULL,"注意", "1312 http 请求状态错误: " + statusCode.toString());
//        return ;
//    }

//    QNetworkReply::NetworkError err = reply->error();

//    if(err != QNetworkReply::NoError){
//        QMessageBox::warning(NULL,"注意", "1312 : " + reply->errorString());
//        return ;
//    }

//    QByteArray bt = reply->readAll();

//    QString t = QString::fromLocal8Bit(bt);

//    QJsonParseError json_error;
//    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
//    if(json_error.error != 0 ){
//        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());
//        return ;
//    }

//    QJsonObject jsob = jsdc.object();

//    if(jsob.value("infcode").toInt() != 0){
//        QString t = jsob.value("err_msg").toString();
//        QMessageBox::warning(NULL,"注意", "1312 交易失败：" + t );
//        return ;
//    }

//    QJsonObject retObj = jsob.value("output").toObject();

//    if(jsob.value("output").toObject().isEmpty()){
//        QMessageBox::warning(NULL,"注意", " 中心查询无结果，无法对照。 ");
//        return ;
//    }

//    int recCount = jsob.value("output").toObject().value("size").toInt();
//    if(recCount == 0){
//        QMessageBox::warning(NULL,"注意", "中心查不到指定的代码，无法对照。 ");
//        return ;
//    }
//    //检查结束


    QJsonDocument d;
    inpu = d.object();
    inpud = d.object();
    putBaseIn(in, "3301");

    inpu.insert("fixmedins_hilist_id", localID);//	定点医药机构目录编号
    inpu.insert("fixmedins_hilist_name", item_name);//	定点医药机构目录名称
    inpu.insert("list_type", list_type);	//目录类别
    inpu.insert("med_list_codg", centerID);	//医疗目录编码 -00110200001000003
    inpu.insert("begndate", QDate::currentDate().toString("yyyy-MM-dd")+" 00:00:00");	//开始日期
    inpu.insert("enddate", "2099-01-01 23:59:59");	//结束日期
    //inpu.insert("aprvno", "210100");	//批准文号
    //inpu.insert("dosform", "210100");	//剂型
    //inpu.insert("exct_cont", "210100");	//除外内容
    //inpu.insert("item_cont", "210100");	//项目内涵
    //inpu.insert("prcunt", "210100");	//计价单位
    //inpu.insert("spec", "210100");	//规格
    //inpu.insert("pacspec", "210100");	//包装规格
    //inpu.insert("memo", "210100");	//备注

    jsar<<inpu;

    inpud.insert("data",jsar);
    in.insert("input", inpud);

    //QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<" 3301 indoc"<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    //ui->leStatus->setText("开始执行医保交易。。。");

    QNetworkReply* reply = naaManager0.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意", "3301 http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意", "3301 : " + reply->errorString());
        return ;
    }

    QMessageBox::information(this,"note","项目对照成功！");
    sq = "insert into fin_com_compare (new_flag,pact_code,his_code,center_code,list_type,"
         " fixmedins_hilist_id, his_name, med_list_codg, fixmedins_hilist_name, begndate,"
         " med_chrgitm_type, center_item_grade,center_name,his_spell) "
         " values ('1','2',lower('"+localID+"'),'"+centerID+"','" + list_type
            +"','" + localID
            +"','" + item_name
            +"','" + centerID
            +"','" + hilist_name
            +"',sysdate, '"
            + med_chrgitm_type
            +"','"+ chrgitm_lv
            +"','"+ hilist_name
            +"','"+ pinyin
            +"')" ;

    if(!qro->exec(sq)){
        QMessageBox::information(this,"note","项目对照本地存储失败！"+qro->lastError().text());
        qDebug()<<qro->lastError().text();
        qDebug()<<qro->lastQuery();
    }


    localID = "";
    centerID = "";

    ui->leLocalDescription->clear();
    ui->leCenterDescription->clear();
    return;

}

void Form_xmdz::on_pbUncompare_clicked()
{
    if(list_type =="999"){
        if(localID.isEmpty()){
            QMessageBox::information(this,"note","未选定本地项目！");
            return;
        }
        sq = "delete from si_bingzhongmulu where"
             " 疾病编码 = '"+ localID +"' and 疾病名称 = '"+item_name+"'";

        if(!qro->exec(sq)){
            QMessageBox::information(this,"note","删除数据失败！");
            qDebug()<<qro->lastQuery();

        }else{
            QMessageBox::information(this,"note","项目数据取撤销完成！");
        }
        return;
    }
    QJsonObject in; //入参
    QJsonObject inpu,inpud; // 入参 input 段

    if(localID.isEmpty() || centerID.isEmpty()){
        QMessageBox::information(this,"note","本地项目或中心未选定！");
        return;
    }
    putBaseIn(in, "3302");

    inpu.insert("fixmedins_code", pub::hos_ybbh);	//医疗目录编码 -00110200001000003
    inpu.insert("fixmedins_hilist_id", localID);//	定点医药机构目录编号
    inpu.insert("list_type", list_type);	//目录类别
    inpu.insert("med_list_codg", centerID);	//医疗目录编码 -00110200001000003
    //inpu.insert("fixmedins_hilist_name", item_name);	//

    inpud.insert("data", inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);
    qDebug()<<" 3302 indoc"<<indoc;
    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }

    QNetworkReply* reply = naaManager0.post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意", "3302 http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意", "3302 : " + reply->errorString());
        return ;
    }


    sq = "delete from fin_com_compare where new_flag = '1' and list_type ='"
            +list_type+"'  and fixmedins_hilist_id ='" + localID+"'";

    if(!qro->exec(sq)){
        QMessageBox::information(this,"note","项目对照本地存储变更失败！"+qro->lastError().text());
        qDebug()<<qro->lastError().text();
        qDebug()<<qro->lastQuery();
    }
    qDebug()<<sq;

    sq = "delete from new_down_1317 where medins_list_codg ='" + localID+"'";
    qDebug()<<sq;
    qrm->exec(sq);


    QMessageBox::information(this,"note","项目对照撤销完成！");
    localID = "";
    centerID = "";

    ui->leLocalDescription->clear();
    ui->leCenterDescription->clear();
    return;
}

void Form_xmdz::on_pbTest_clicked()
{
    display("");
}

void Form_xmdz::putBaseIn( QJsonObject &in, QString code)
{
    QString inp;
    inp = pub::getYbStrJ(code,"");   //
    if(inp.isEmpty()){
        pub::useInsurance = false;  //设置医保不可用
        QMessageBox::information(this,"note","申请交易流水号失败！");
        return; //未能成功申请交易流水号
    }
    currentMsgid = inp;
    currentInfno = code;
    in.insert("infno", code);
    in.insert( "msgid", inp);
    in.insert("mdtrtarea_admvs", pub::hos_insuplc);

    in.insert("insuplc_admdvs","210100");
    in.insert("recer_sys_code", "OwnSoft");
    in.insert("dev_no","");
    in.insert("dev_safe_info","");
    in.insert("cainfo", pub::hos_secretcode);
    in.insert("signtype","");
    in.insert("infver", "V1.0");
    in.insert("opter_type", "1");
    in.insert("opter", pub::usr_code);
    in.insert("opter_name", pub::usr_name);
    in.insert("inf_time", QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    in.insert("fixmedins_code", pub::hos_ybbh);
    in.insert("fixmedins_name", pub::hos_name);
    in.insert("sign_no", pub::sign_no);

}

void Form_xmdz::on_tvDisplay_doubleClicked(const QModelIndex &index)
{

    if(ui->rbLocal->isChecked()){
        localID = index.siblingAtColumn(0).data(0).toString();
        item_name = index.siblingAtColumn(1).data(0).toString();
        ui->leLocalDescription->setText(localID + "/"
                                        + item_name+ "/"
                                        + index.siblingAtColumn(2).data(0).toString());
        //ui->lePinyin->setText(index.siblingAtColumn(2).data(0).toString());
    }else if(ui->rbCenter->isChecked()){

        if(list_type =="999"){
            centerID = index.siblingAtColumn(5).data(0).toString();
            hilist_name = index.siblingAtColumn(4).data(0).toString();
            ui->leCenterDescription->setText(centerID + "/" + hilist_name);

            qro->exec("select get_spell('" + hilist_name + "') from dual");
            qro->next();
            ui->lePinyin->setText(qro->value(0).toString());
            return;
        }else { //if(list_type =="101"){
            //医疗目录编码, 注册名称, 批准文号, 生产企业名称, 药品规格, 最小包装数量, 最小包装单位
            centerID = index.siblingAtColumn(0).data(0).toString();
            hilist_name = index.siblingAtColumn(1).data(0).toString();

            sq = "select hilist_code, hilist_name, med_chrgitm_type, chrgitm_lv, lmt_used_flag, "
                 " med_use_flag, hilist_use_type, vali_flag, pinyin "
                 " from new_down_1312 where vali_flag = '1' and list_type = '"
                    + list_type + "' and  hilist_code = '" + centerID + "' ";
            qrm->exec(sq);
            if(qrm->next()){
                chrgitm_lv = qrm->value(3).toString();
                lmt_used_flag = qrm->value(4).toString();
                med_use_flag = qrm->value(5).toString();
                hilist_use_type = qrm->value(6).toString();
                pinyin = qrm->value(8).toString();
                ui->leCenterDescription->setText(centerID
                                                 + "/"
                                                 + hilist_name
                                                 + "/"
                                                 + index.siblingAtColumn(2).data(0).toString());
                med_chrgitm_type = qrm->value(2).toString();
            }else{
                QMessageBox::information(this,"注意","医保目录中找不到指定的记录!");
                return;
            }

        }
    }else{
        if(list_type !="999"){
            localID = index.siblingAtColumn(0).data(0).toString();
            centerID = index.siblingAtColumn(2).data(0).toString();
            ui->leLocalDescription->setText(localID + "/" + index.siblingAtColumn(1).data(0).toString());
            ui->leCenterDescription->setText(centerID + "/" + index.siblingAtColumn(3).data(0).toString());
        }
    }

}

void Form_xmdz::postRequestFinished(QNetworkReply *)
{
    qDebug()<<"http return ! ";

    httpFinished = true;
    QVariant statusCode = rpl->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意", " 3301/3302 http 请求状态错误: " + statusCode.toString());
        httpSuccess = false;
        return;
    }

    QNetworkReply::NetworkError err = rpl->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意", "3301/3302 " + rpl->errorString());
        httpSuccess = false;
        return;
    }else{

        httpSuccess = true;

    }
}

void Form_xmdz::on_leSearch_returnPressed()
{
    display( ui->leSearch->text().trimmed());
}

void Form_xmdz::on_pbMlcx_clicked()
{

    if(centerID.isEmpty()){
        QMessageBox::information(this,"错误","未指定中心项目代码！");
        return;
    }

    QString jydm ;   //交易代码查询目录是否存在

    if(ui->rb1312->isChecked()){
        jydm = "1312";
    }else if(ui->rb1316->isChecked()){
        jydm = "1316";
    }else if(ui->rb1318->isChecked()){
        jydm = "1318";
    }

    QString startDate = "2000-01-01 00:00:00";
    QJsonObject in;

    putBaseIn(in, jydm);

    if(currentMsgid.isEmpty()){
        QMessageBox::information(this,"错误","申请交易流水号失败！");
        return ;
    }

    QJsonObject inpu,inpud;

    if( jydm == "1312" or jydm == "1316" ){

        inpu.insert("hilist_code", centerID);//项目代码
        inpu.insert("updt_time", startDate);   //更新时间
        inpu.insert("page_num", 1);    //当前页数
        inpu.insert("page_size", 10);   //本页数据量
        inpu.insert("vali_flag", "1");   //
        inpu.insert("insu_admdvs", pub::hos_insuplc);   //

    }else if( jydm == "1318" ){

        inpu.insert("hilist_code", centerID);//项目代码
        inpu.insert("updt_time", startDate);   //更新时间
        inpu.insert("page_num", 1);    //当前页数
        inpu.insert("page_size", 10);   //本页数据量

    }


    inpud.insert("data",inpu);
    in.insert("input", inpud);

    QJsonDocument indoc ;
    indoc.setObject(in);

    qDebug()<<jydm +" 交易入参: "<<indoc;

    //保存入参日志
    sq = "update com_business set v_in = '"
            + indoc.toJson(QJsonDocument::Compact)
            + "' where jyls = '" + currentMsgid + "'";

    if(!qrm->exec(sq)){
        QMessageBox::information(this,"错误","保存入参日志失败！");
        qDebug()<<sq;
    }
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QNetworkReply* reply = manager->post(request,indoc.toJson());

    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); // 进入等待返回

    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    if(!statusCode.isValid()){
        QMessageBox::warning(NULL,"注意", jydm + " http 请求状态错误: " + statusCode.toString());
        return ;
    }

    QNetworkReply::NetworkError err = reply->error();

    if(err != QNetworkReply::NoError){
        QMessageBox::warning(NULL,"注意",jydm + " : " + reply->errorString());
        return ;
    }

    QByteArray bt = reply->readAll();

    QString t = QString::fromLocal8Bit(bt);

    QJsonParseError json_error;
    QJsonDocument jsdc = QJsonDocument::fromJson(t.toUtf8(), &json_error);
    if(json_error.error != 0 ){
        QMessageBox::warning(NULL,"注意","返回信息解析错误 " + json_error.errorString());
        return ;
    }

    QJsonObject jsob = jsdc.object();

    if(jsob.value("infcode").toInt() != 0){
        QString t = jsob.value("err_msg").toString();
        QMessageBox::warning(NULL,"注意", jydm + " 交易失败：" + t );
        return ;
    }

    QJsonObject retObj = jsob.value("output").toObject();
    QString result;
    if(jydm == "1312" ){
        result.append("hilist_name\tbegndate\tenddate\tvali_flag\tpool_area");
        if(jsob.value("output").toObject().isEmpty()){
            QMessageBox::warning(NULL,"注意", jydm + "返回信息为空 ");
            return ;
        }


        int recCount = jsob.value("output").toObject().value("size").toInt();
        if(recCount == 0){
            QMessageBox::warning(NULL,"注意", jydm + " 查询无结果 ");
            return ;
        }

        QJsonArray retData = jsob.value("output").toObject().value("data").toArray();
        while(recCount > 0){
            recCount--;
            QJsonObject ob = retData[recCount].toObject();
            result.append("\r\n");
            result.append(ob.value("hilist_name").toString());
            result.append("\t");
            result.append(ob.value("begndate").toString());
            result.append("\t");
            result.append(ob.value("enddate").toString());
            result.append("\t");
            result.append(ob.value("vali_flag").toString());
            result.append("\t");
            result.append(ob.value("poolarea_no").toString());

        }
    }else if(jydm == "1316"){
        result.append("vali_flag  ");
        if(jsob.value("output").toObject().isEmpty()){
            QMessageBox::warning(NULL,"注意", jydm + "返回信息为空 ");
            return ;
        }

        int recCount = jsob.value("output").toObject().value("size").toInt();
        if(recCount == 0){
            QMessageBox::warning(NULL,"注意", jydm + " 查询无结果 ");
            return ;
        }

        QJsonArray retData = jsob.value("output").toObject().value("data").toArray();
        while(recCount > 0){
            recCount--;
            QJsonObject ob = retData[recCount].toObject();
            result.append("\r\n");
            result.append(ob.value("vali_flag").toString());


        }
    } else if(jydm == "1318"){
        if(jsob.value("output").toObject().isEmpty()){

            QMessageBox::warning(NULL,"注意", jydm + "返回信息为空 ");
            return ;
        }


        int recCount = jsob.value("output").toObject().value("size").toInt();
        if(recCount == 0){

            QMessageBox::warning(NULL,"注意", jydm + " 查询无结果 ");
            return ;
        }

        QJsonArray retData = jsob.value("output").toObject().value("data").toArray();
        while(recCount > 0){
            recCount--;
            QJsonObject ob = retData[recCount].toObject();
            result.append("\r\n");
            result.append(ob.value("hilist_code").toString());
            result.append("\t");
            result.append(ob.value("hilist_lmtpric_type").toString());
            result.append("\t");
            result.append(ob.value("overlmt_dspo_way").toString());
            result.append("\t");
            result.append(QString::asprintf("%.2f",ob.value("hilist_pric_uplmt_amt").toDouble()));
            result.append("\t");
            result.append(ob.value("tabname").toString());

        }
    }
    QMessageBox::warning(NULL,"注意", result);
}
